/*
 * EditorForm.cpp
 * Authors: 107 <107@mail.ru>
 * License: GPL >=2.0
 *
 *
 * Simple editor form with keyboard and EditArea
 * Provide Set and Get text from/to EditArea.
 *
 * Call backend on some actions. Only backend known what to do with text
 *
 */

#include "EditorForm.h"
#include "macros.h"

// XXX: Platform-dependence
#define EDIT_MAX_LEN 990

using namespace Osp::Base;
using namespace Osp::Ui;
using namespace Osp::Graphics;
using namespace Osp::Ui::Controls;
using namespace badadev;

EditorForm::EditorForm( void ) {
}

EditorForm::~EditorForm( void ) {
}

result EditorForm::Initialize( InheritanceContainer &parent )		{
			result r;
			this->Construct(FORM_STYLE_SOFTKEY_0 | FORM_STYLE_SOFTKEY_1 | !FORM_STYLE_TITLE);

			AddOrientationEventListener( *this );
			SetOrientation(ORIENTATION_AUTOMATIC_FOUR_DIRECTION);

			this->setParent(parent);

			__pPanel1 = new ScrollPanel();
			__pPanel1->Construct( Rectangle( 0,0, GetWidth(), GetHeight() ) );

			__pEdit1 = new EditArea();
			__pEdit1->Construct( Rectangle( 0,0, GetWidth(), GetHeight() ),
										INPUT_STYLE_OVERLAY,
									  EDIT_MAX_LEN );
			__pEdit1->AddActionEventListener( *this );
			__pEdit1->SetOverlayKeypadCommandButton(COMMAND_BUTTON_POSITION_LEFT, S("IDS_DONE"), ID_EDITOR_SAVE );
			__pEdit1->SetOverlayKeypadCommandButton(COMMAND_BUTTON_POSITION_RIGHT, S("IDS_CANCEL"), ID_EDITOR_CLOSE );
			__pPanel1->AddControl( *__pEdit1 );
		    this->AddControl( *__pPanel1 );

}



void EditorForm::Resize ( void ) {
	__pPanel1->SetSize( this->GetSize() );
	__pEdit1->SetSize( this->GetSize() );
	RequestRedraw();
}

Osp::Base::String EditorForm::GetText ( void ) {
	return __pEdit1->GetText();
}

void EditorForm::SetText ( const Osp::Base::String & text ) {
	__pEdit1->SetText( text );
	__pEdit1->SetCursorPosition( 0 );
	__pEdit1->ShowKeypad();
}

void EditorForm::OnActionPerformed(const Osp::Ui::Control& source, int actionId) {
	switch(actionId) 	{

		case ID_EDITOR_SAVE: {
			__pPanel1->CloseOverlayWindow();
			this->SetShowState(false);
			this->sendContainerActionToParent( ID_EDITOR_SAVE );
		};
		break;

		case ID_EDITOR_CLOSE: {
			__pPanel1->CloseOverlayWindow();
			this->SetShowState(false);
			this->sendContainerActionToParent( ID_EDITOR_CLOSE );
		};
		break;


	}
}

void EditorForm::OnOrientationChanged(const Osp::Ui::Control &source, Osp::Ui::OrientationStatus orientationStatus) {
	Resize();
}
